package com.originskyseed.platform.user.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.user.mapper.*;
import com.originskyseed.platform.user.model.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.service.ISalesmanLevelService;

/**
 * 业务员等级体系Service接口实现类（OSI_SALESMAN_LEVEL）
 *
 * @author Tangwanghui
 */
@DubboService
@Component("salesmanLevelaService")
public class SalesmanLevelServiceImpl implements ISalesmanLevelService {

	@Resource
	private SalesmanLevelMapper salesmanLevelaMapper;

	@Resource
	private PlayerRelateRechargeMapper playerRelateRechargeMapper;

	@Resource
	private SalesmanRelateLevelLogMapper salesmanRelateLevelLogMapper;

	@Resource
	private SalesmanRelateLevelMapper salesmanRelateLevelMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	@Resource
	private LevelTravelMapper levelTravelMapper;

	@Resource
	private DeptMapper deptMapper;

	@Resource
	private AnchorLevelMapper anchorLevelMapper;

	@Resource
	private AnchorRelateLevelMapper anchorRelateLevelMapper;

	@Resource
	private PlayerRelateConsumeMapper playerRelateConsumeMapper;

	@Resource
	private AnchorLevelLogMapper anchorLevelLogMapper;

	@Resource
	private LevelSystemRelDeptMapper levelSystemRelDeptMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.salesmanLevelaMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.salesmanLevelaMapper.deleteByPrimaryKeys(ids);
	}


	/**
	 * 批量修改删除的状态标识
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public int updateByIds(List<String> ids) throws Exception
	{
		return this.salesmanLevelaMapper.updateByIds(ids,new Date());
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(SalesmanLevel record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.salesmanLevelaMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(SalesmanLevel record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.salesmanLevelaMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(SalesmanLevel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.salesmanLevelaMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(SalesmanLevel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.salesmanLevelaMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public SalesmanLevel selectByPrimaryKey(String id) throws Exception {
		return this.salesmanLevelaMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<SalesmanLevel> selectList(SalesmanLevel record) throws Exception {
		return this.salesmanLevelaMapper.selectList(record);
	}

	/**
	 * 根据公司获取所属等级信息
	 *
	 * @param deptId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<SalesmanLevel> selectListByDeptId(String deptId) throws Exception {
		Dept dept = this.deptMapper.selectByPrimaryKey(deptId);
		String tempId="";
		if (dept != null) {
			Dept resultDept = this.selectTopDept(dept.getCode());
			if(resultDept!=null)
			{
				tempId=resultDept.getId();
			}
		}
		if(tempId==null||StringUtils.isBlank(tempId))
		{
			return new ArrayList<>();
		}
		List<SalesmanLevel> list=this.salesmanLevelaMapper.selectListByDeptId(tempId);
		if(list==null||list.size()==0)
        {
            list=new ArrayList<>();
            List<AnchorLevel> anchorLevelList=this.anchorLevelMapper.selectListByDeptId(tempId);
            if(anchorLevelList!=null&&anchorLevelList.size()>0)
            {
                for(int i=0;i<anchorLevelList.size();i++)
                {
                    SalesmanLevel salesmanLevel=new SalesmanLevel();
                    salesmanLevel.setId(anchorLevelList.get(i).getId());
                    salesmanLevel.setLevelName(anchorLevelList.get(i).getLevelName());

                    list.add(salesmanLevel);
                }
            }
        }
		return list;
	}

	/**
	 * 递归查找部门的顶级
	 *
	 * @return
	 */
	private Dept selectTopDept(String code) throws Exception {
		if (code == null || StringUtils.isBlank(code)) {
			return null;
		}
		Dept dept = this.deptMapper.selectByCode(code);
		if (dept == null) {
			return null;
		}
		if (dept.getType() != null&&dept.getType()==2) {
			return dept;
		}
		else {
			return selectTopDept(dept.getParentCode());
		}
	}



	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<SalesmanLevel> selectListByPage(Map<String, Object> params, BasePage<SalesmanLevel> page) throws Exception {
		params.put("page", page);
		List<SalesmanLevel> list = this.salesmanLevelaMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询下一等级
	 *
	 * @param maxValue
	 * @return
	 * @throws Exception
	 */
	@Override
	public SalesmanLevel selectNexLevel(BigDecimal maxValue) throws Exception {
		return this.salesmanLevelaMapper.selectNexLevel(maxValue);
	}

	/**
	 * pk选择业务员pk对象分页列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PkObjUserInfoVO> selectPkSalesManObjListByPage(Map<String, Object> params, BasePage<PkObjUserInfoVO> page, String loginUser) throws Exception {
		params.put("page", page);
		List<PkObjUserInfoVO> list = this.salesmanLevelaMapper.selectPkSalesManObjListByPage(params);
		// 移除我的
		if (list != null && list.size() > 0) {
			for (int i = 0, len = list.size(); i < len; i++) {
				PkObjUserInfoVO pkObjUserInfoVO = list.get(i);
				if (loginUser.equals(pkObjUserInfoVO.getToUserId())) {
					list.remove(i);
					len--;
					i--;
					continue;
				}
				list.set(i, pkObjUserInfoVO);
			}
		}
		page.setResults(list);
		return page;
	}

	/**
	 * pk选择主播pk对象分页列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PkObjUserInfoVO> selectPkAnchorObjListByPage(Map<String, Object> params, BasePage<PkObjUserInfoVO> page, String loginUser) throws Exception {
		params.put("page", page);
		List<PkObjUserInfoVO> list = this.salesmanLevelaMapper.selectPkAnchorObjListByPage(params);
		// 移除我的
		if (list != null && list.size() > 0) {
			for (int i = 0, len = list.size(); i < len; i++) {
				PkObjUserInfoVO pkObjUserInfoVO = list.get(i);
				if (loginUser.equals(pkObjUserInfoVO.getToUserId())) {
					list.remove(i);
					len--;
					i--;
					continue;
				}
				list.set(i, pkObjUserInfoVO);
			}
		}
		page.setResults(list);
		return page;
	}

	/**
	 * pk选择体系pk对象分页列表
	 * 
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PkObjUserInfoVO> selectPkDeptObjListByPage(Map<String, Object> params, BasePage<PkObjUserInfoVO> page, UserInfo userInfo) throws Exception {
		params.put("page", page);
		if (userInfo != null && userInfo.getDeptId() != null) {
			Dept dept = this.deptMapper.selectByPrimaryKey(userInfo.getDeptId());
			if (dept != null) {
				params.put("sourceSys", dept.getSourceSys());
				params.put("type", dept.getType());
				params.put("removeMyDeptId", dept.getId());// 移除我的部门信息
			}
		}
		List<PkObjUserInfoVO> list = this.salesmanLevelaMapper.selectPkDeptObjListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 修改峰值
	 * 
	 * @param peakValue
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updatePeakValue(Integer peakValue) throws Exception {
		return this.salesmanLevelaMapper.updatePeakValue(peakValue);
	}

	/**
	 * 查询经验值是否存在重复
	 * 
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean selectEmpiricValue(SalesmanLevel record) throws Exception {
		return this.salesmanLevelaMapper.selectEmpiricValue(record);
	}

	/**
	 * 根据能量值查询等级id
	 * 
	 * @param empiricValue
	 * @return
	 * @throws Exception
	 */
	@Override
	public String selectLevelIdByEmpiricValue(BigDecimal empiricValue,String belongDeptId) throws Exception {
		return this.salesmanLevelaMapper.selectLevelIdByEmpiricValue(empiricValue,belongDeptId);
	}

	/**
	 * 定时更新用户的等级
	 * 
	 * @param tenantCode
	 * @throws Exception
	 */
	public void insertSalesLevel(String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		insertSalesmanLevel();
		insertAnchorLevel();
	}

	/**
	 * 同步业务员等级数据
	 * @throws Exception
	 */
	private void insertSalesmanLevel() throws Exception{
		// 查询所有等级
		List<SalesmanLevel> salesmanLevelList = this.salesmanLevelaMapper.selectList(new SalesmanLevel());

		if(salesmanLevelList.size()>0){
			// 对等级分组，每个等级体系一组
			Map<String,List<SalesmanLevel>> listMap = new HashMap<>();
			for (SalesmanLevel salesmanLevela:salesmanLevelList) {
				boolean flag = true;
				for (String levelSystemId:listMap.keySet()) {
					if(levelSystemId.equals(salesmanLevela.getSystemId())){
						flag = false;
						List<SalesmanLevel> list = listMap.get(salesmanLevela.getSystemId());
						list.add(salesmanLevela);
						listMap.put(salesmanLevela.getSystemId(),list);
						break;
					}
				}
				if(flag){
					List<SalesmanLevel> addList = new ArrayList<>();
					addList.add(salesmanLevela);
					listMap.put(salesmanLevela.getSystemId(),addList);
				}
			}
			// 等级日志
			List<SalesmanRelateLevelLog> salesmanRelateLevelLogList = new ArrayList<>();

			// 查询业务员
			UserInfo query = new UserInfo();
			query.setType(6);
			query.setSourceSys(2);
			List<UserInfo> userInfoList = this.userInfoMapper.selectList(query);
			for (UserInfo userInfo : userInfoList) {
				// 查询用户的分公司id
				String belongDeptId = this.selectBelongDeptId(userInfo.getDeptId());
				// 查询分公司对应的等级体系id
				String levelSystemId = this.levelSystemRelDeptMapper.selectLevelSystemIdByDeptId(belongDeptId);
				List<SalesmanLevel> salesmanLevelaList = listMap.get(levelSystemId);
				if(salesmanLevelaList!=null){
					SalesmanRelateLevel salesmanRelateLevel = this.salesmanRelateLevelMapper.selectByAccount(userInfo.getAccount());
					if (salesmanRelateLevel == null) {
						salesmanRelateLevel = new SalesmanRelateLevel();
						salesmanRelateLevel.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
						salesmanRelateLevel.setEmpiricValue(BigDecimal.ZERO);
						salesmanRelateLevel.setStaffAccount(userInfo.getAccount());
						salesmanRelateLevel.setStaffName(userInfo.getName());
						salesmanRelateLevel.setLevelId(getSalesmanLevelIdByValue(salesmanLevelaList, BigDecimal.ZERO));
						salesmanRelateLevel.setLevelName(getSalesmanLevelNameByValue(salesmanLevelaList, BigDecimal.ZERO));
						salesmanRelateLevel.setCreateTime(new Date());
						if(StringUtils.isNotBlank(salesmanRelateLevel.getLevelId()) ){
							this.salesmanRelateLevelMapper.insertSelective(salesmanRelateLevel);
						}
					}

					Map<String, Object> queryMap = new HashMap<>();
					queryMap.put("channelSubId", userInfo.getOtherAccount());
					List<PlayerRelateRecharge> playerRelateRechargeList = this.playerRelateRechargeMapper.selectRechargeByLevelLog(queryMap);
					for (PlayerRelateRecharge playerRelateRecharge : playerRelateRechargeList) {
						SalesmanRelateLevelLog salesmanRelateLevelLog = new SalesmanRelateLevelLog();
						salesmanRelateLevelLog.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
						salesmanRelateLevelLog.setOldExpValue(salesmanRelateLevel.getEmpiricValue());
						salesmanRelateLevelLog.setOldLevelName(salesmanRelateLevel.getLevelName()==null ?"":salesmanRelateLevel.getLevelName());
						salesmanRelateLevelLog.setExpValue(salesmanRelateLevel.getEmpiricValue().add(playerRelateRecharge.getAmount()));
						String levelName = getSalesmanLevelNameByValue(salesmanLevelaList, salesmanRelateLevelLog.getExpValue());
						if (levelName == null) {
							salesmanRelateLevelLog.setLevelName(salesmanRelateLevel.getLevelName());
						} else {
							salesmanRelateLevelLog.setLevelName(levelName);
						}
						salesmanRelateLevelLog.setBusinessId(userInfo.getId());
						salesmanRelateLevelLog.setRechargeId(playerRelateRecharge.getOrderId());
						salesmanRelateLevelLog.setCreateTime(playerRelateRecharge.getRechargeTime());
						salesmanRelateLevelLog.setCreator("系统同步");
						salesmanRelateLevelLogList.add(salesmanRelateLevelLog);

						// 改变业务员等级的经验值
						String oldLevelId = salesmanRelateLevel.getLevelId();
						String newLevelId = getSalesmanLevelIdByValue(salesmanLevelaList, salesmanRelateLevelLog.getExpValue());
						if (oldLevelId != null && newLevelId != null && !oldLevelId.equals(newLevelId)) {
							LevelTravel levelTravel = new LevelTravel();
							levelTravel.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
							levelTravel.setAccount(userInfo.getAccount());
							levelTravel.setEmpiricValue(salesmanRelateLevelLog.getExpValue());
							levelTravel.setLevelId(newLevelId);
							levelTravel.setLevelName(getSalesmanLevelNameByValue(salesmanLevelaList, salesmanRelateLevelLog.getExpValue()));
							levelTravel.setType(0);
							levelTravel.setUserId(userInfo.getId());
							levelTravel.setCreateTime(new Date());
							this.levelTravelMapper.insertSelective(levelTravel);
						}
						salesmanRelateLevel.setEmpiricValue(salesmanRelateLevelLog.getExpValue());
						if (levelName != null) {
							salesmanRelateLevel.setLevelName(getSalesmanLevelNameByValue(salesmanLevelaList, salesmanRelateLevelLog.getExpValue()));
						}
						if (newLevelId != null) {
							salesmanRelateLevel.setLevelId(newLevelId);
							userInfo.setSalesmanLevelId(getSalesmanLevelIdByValue(salesmanLevelaList, salesmanRelateLevelLog.getExpValue()));
						}
					}
					this.salesmanRelateLevelMapper.updateByPrimaryKeySelective(salesmanRelateLevel);
					this.userInfoMapper.updateByPrimaryKeySelective(userInfo);
				}
			}

			int count = 0;
			// 一次性插入的数量
			int size = 500;
			// 总长度
			int length = salesmanRelateLevelLogList.size();
			// 计算可以分成多少组
			int num = (length + size - 1) / size;
			for (int i = 0; i < num; i++) {
				// 开始位置
				int fromIndex = i * size;
				// 结束位置
				int toIndex = (i + 1) * size < length ? (i + 1) * size : length;
				List<SalesmanRelateLevelLog> newList = salesmanRelateLevelLogList.subList(fromIndex, toIndex);
				count += salesmanRelateLevelLogMapper.insertBatch(newList);
			}
		}
	}


	/**
	 * 同步主播等级数据
	 * @throws Exception
	 */
	private void insertAnchorLevel() throws Exception{
		// 查询所有等级
		List<AnchorLevel> anchorLevelList = this.anchorLevelMapper.selectList(new AnchorLevel());

		if(anchorLevelList.size()>0){
			// 对等级分组，每个分公司一组
			Map<String,List<AnchorLevel>> listMap = new HashMap<>();
			for (AnchorLevel anchorLevel:anchorLevelList) {
				boolean flag = true;
				for (String belongDeptId:listMap.keySet()) {
					if(belongDeptId.equals(anchorLevel.getSystemId())){
						flag = false;
						List<AnchorLevel> list = listMap.get(anchorLevel.getSystemId());
						list.add(anchorLevel);
						listMap.put(anchorLevel.getSystemId(),list);
						break;
					}
				}
				if(flag){
					List<AnchorLevel> addList = new ArrayList<>();
					addList.add(anchorLevel);
					listMap.put(anchorLevel.getSystemId(),addList);
				}
			}
			// 等级日志
			List<AnchorLevelLog> anchorLevelLogList = new ArrayList<>();

			// 查询主播
			UserInfo query = new UserInfo();
			query.setType(11);
			query.setSourceSys(1);
			List<UserInfo> userInfoList = this.userInfoMapper.selectList(query);
			for (UserInfo userInfo : userInfoList) {
				// 查询用户的分公司id
				String belongDeptId = this.selectBelongDeptId(userInfo.getDeptId());
				// 查询分公司对应的等级体系id
				String levelSystemId = this.levelSystemRelDeptMapper.selectLevelSystemIdByDeptId(belongDeptId);
				List<AnchorLevel> list = listMap.get(levelSystemId);
				if(list!=null){
					AnchorRelateLevel anchorRelateLevel = this.anchorRelateLevelMapper.selectByAccount(userInfo.getAccount());
					if (anchorRelateLevel == null) {
						anchorRelateLevel = new AnchorRelateLevel();
						anchorRelateLevel.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
						anchorRelateLevel.setEmpiricValue(BigDecimal.ZERO);
						anchorRelateLevel.setAnchorId(userInfo.getAccount());
						anchorRelateLevel.setLevelId(getAnchorLevelIdByValue(list, BigDecimal.ZERO));
						anchorRelateLevel.setLevelName(getAnchorLevelNameByValue(list,BigDecimal.ZERO));
						anchorRelateLevel.setCreateTime(new Date());
						if(StringUtils.isNotBlank(anchorRelateLevel.getLevelId()) ){
							this.anchorRelateLevelMapper.insertSelective(anchorRelateLevel);
						}
					}

					if(StringUtils.isNotBlank(userInfo.getOtherAccount())){
						PlayerRelateConsume playerRelateConsumeQuery = new PlayerRelateConsume();
						playerRelateConsumeQuery.setChannelSubId(userInfo.getOtherAccount());
						List<PlayerRelateConsume> playerRelateConsumeList = this.playerRelateConsumeMapper.selectList(playerRelateConsumeQuery);
						for (PlayerRelateConsume playerRelateConsume : playerRelateConsumeList) {
							AnchorLevelLog anchorLevelLog = new AnchorLevelLog();
							anchorLevelLog.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
							anchorLevelLog.setOldExpValue(anchorRelateLevel.getEmpiricValue());
							anchorLevelLog.setOldLevelName(anchorRelateLevel.getLevelName()==null ?"":anchorRelateLevel.getLevelName());
							anchorLevelLog.setExpValue(anchorRelateLevel.getEmpiricValue().add(playerRelateConsume.getAmount()));
							String levelName = getAnchorLevelNameByValue(anchorLevelList, anchorLevelLog.getExpValue());
							if (levelName == null) {
								anchorLevelLog.setLevelName(anchorRelateLevel.getLevelName());
							} else {
								anchorLevelLog.setLevelName(levelName);
							}
							anchorLevelLog.setAnchorId(userInfo.getId());
							anchorLevelLog.setCreateTime(playerRelateConsume.getConsumeTime());
							anchorLevelLog.setCreator("系统同步");
							anchorLevelLogList.add(anchorLevelLog);

							// 改变业务员等级的经验值
							String oldLevelId = anchorRelateLevel.getLevelId();
							String newLevelId = getAnchorLevelIdByValue(anchorLevelList, anchorLevelLog.getExpValue());
							if (oldLevelId != null && newLevelId != null && !oldLevelId.equals(newLevelId)) {
								LevelTravel levelTravel = new LevelTravel();
								levelTravel.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
								levelTravel.setAccount(userInfo.getAccount());
								levelTravel.setEmpiricValue(anchorLevelLog.getExpValue());
								levelTravel.setLevelId(newLevelId);
								levelTravel.setLevelName(getAnchorLevelNameByValue(anchorLevelList, anchorLevelLog.getExpValue()));
								levelTravel.setType(1);
								levelTravel.setUserId(userInfo.getId());
								levelTravel.setCreateTime(new Date());
								this.levelTravelMapper.insertSelective(levelTravel);
							}
							anchorRelateLevel.setEmpiricValue(anchorLevelLog.getExpValue());
							if (levelName != null) {
								anchorLevelLog.setLevelName(getAnchorLevelNameByValue(anchorLevelList, anchorLevelLog.getExpValue()));
							}
							if (newLevelId != null) {
								anchorRelateLevel.setLevelId(newLevelId);
								userInfo.setSalesmanLevelId(getAnchorLevelIdByValue(anchorLevelList, anchorLevelLog.getExpValue()));
							}
						}
						this.anchorRelateLevelMapper.updateByPrimaryKeySelective(anchorRelateLevel);
						this.userInfoMapper.updateByPrimaryKeySelective(userInfo);
					}
				}
			}

			int count = 0;
			// 一次性插入的数量
			int size = 500;
			// 总长度
			int length = anchorLevelLogList.size();
			// 计算可以分成多少组
			int num = (length + size - 1) / size;
			for (int i = 0; i < num; i++) {
				// 开始位置
				int fromIndex = i * size;
				// 结束位置
				int toIndex = (i + 1) * size < length ? (i + 1) * size : length;
				List<AnchorLevelLog> newList = anchorLevelLogList.subList(fromIndex, toIndex);
				count += anchorLevelLogMapper.insertBatch(newList);
			}
		}
	}

	/**
	 * 获取业务员等级id
	 * 
	 * @param salesmanLevelaList
	 * @param value
	 * @return
	 */
	private String getSalesmanLevelIdByValue(List<SalesmanLevel> salesmanLevelaList, BigDecimal value) {
		String levelId = null;
		for (SalesmanLevel salesmanLevela : salesmanLevelaList) {
			if (value.compareTo(salesmanLevela.getEmpiricValueMin()) >= 0) {
				levelId = salesmanLevela.getId();
			}
		}
		return levelId;
	}

	/**
	 * 获取业务员等级名称
	 * 
	 * @param salesmanLevelaList
	 * @param value
	 * @return
	 */
	private String getSalesmanLevelNameByValue(List<SalesmanLevel> salesmanLevelaList, BigDecimal value) {
		String levelName = null;
		for (SalesmanLevel salesmanLevela : salesmanLevelaList) {
			if (value.compareTo(salesmanLevela.getEmpiricValueMin()) >= 0) {
				levelName = salesmanLevela.getLevelName();
			}
		}
		return levelName;
	}

	/**
	 * 获取主播等级id
	 *
	 * @param anchorLevelList
	 * @param value
	 * @return
	 */
	private String getAnchorLevelIdByValue(List<AnchorLevel> anchorLevelList, BigDecimal value) {
		String levelId = null;
		for (AnchorLevel anchorLevel : anchorLevelList) {
			if (value.compareTo(anchorLevel.getEmpiricValueMin()) >= 0) {
				levelId = anchorLevel.getId();
			}
		}
		return levelId;
	}

	/**
	 * 获取主播等级名称
	 *
	 * @param anchorLevelList
	 * @param value
	 * @return
	 */
	private String getAnchorLevelNameByValue(List<AnchorLevel> anchorLevelList, BigDecimal value) {
		String levelName = null;
		for (AnchorLevel anchorLevel : anchorLevelList) {
			if (value.compareTo(anchorLevel.getEmpiricValueMin()) >= 0) {
				levelName = anchorLevel.getLevelName();
			}
		}
		return levelName;
	}

	/**
	 * 查询分公司id
	 * @param deptId
	 * @return
	 * @throws Exception
	 */
	private String selectBelongDeptId(String deptId) throws Exception{
		Dept dept = this.deptMapper.selectByPrimaryKey(deptId);
		if(dept!=null){
			if(dept.getType()==2){
				return dept.getId();
			}else {
				Dept parentDept = this.deptMapper.selectByCode(dept.getParentCode());
				if(parentDept!=null){
					return selectBelongDeptId(parentDept.getId());
				}else {
					return null;
				}
			}
		}else {
			return null;
		}
	}

}
